/**
 * Copyright (c) 2020-2024, huli.com
 * All rights reserved.
 */
package com.xiaohujr.xuexue.leetcode.interview;

/**
 * @author Xue Xue (xuexue1@huli.com)
 * @version 1.0
 * @since 2021/1/25
 */
public class QuickSort {

    public static void quickSort(int[] arr, int low, int high) {
        int i, j, temp, t;
        if (low >= high) {
            return;
        }
        i = low;
        j = high;
        temp = arr[low];
        while (i < j) {

            while (temp >= arr[i] && i < j) {
                i++;
            }
            while (temp <= arr[j] && i < j) {
                j--;
            }
            if (i < j) {
                t = arr[j];
                arr[j] = arr[i];
                arr[i] = t;
            }
        }
        arr[low] = arr[i];
        arr[i] = temp;

        quickSort(arr, low, i - 1);
        quickSort(arr, i + 1, high);
    }

    public static void main(String[] args) {
        int[] arr = {1, 7, 2, 4};
        quickSort(arr, 0, arr.length - 1);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}
